﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// them thu vien
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using QuanLyChoVayCaNhan.Models;
using System.Threading;

namespace QuanLyChoVayCaNhan.Classes
{
    public class PrintManager
    {
        // static
        private static PrintManager INSTANCE = new PrintManager();

        public static PrintManager GetInstance()
        {
            return INSTANCE;
        }

        // const
        private const string TEMPLATES_FOLDER_PATH = @"D:\Work\Trang Project\Trang's Project\QuanLyChoVayCaNhan\bin\Debug\Templates\";
        private const string BAO_CAO_THAM_DINH_TIN_DUNG_TEMPLATE_PATH = TEMPLATES_FOLDER_PATH + @"baocaothamdinh.doc";
        private const string BIEN_BAN_DINH_GIA_TAI_SAN_TEMPLATE_PATH = TEMPLATES_FOLDER_PATH + @"bienbandinhgiataisan.doc";
        private const string GIAY_TIEP_NHAN_HO_SO_TEMPLATE_PATH = TEMPLATES_FOLDER_PATH + @"giaytiepnhanhosokhachhang.doc";
        // var
        private object missing = Missing.Value;


        private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiactitics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            wordApp.Selection.Find.Execute(ref findText,
                        ref matchCase, ref matchWholeWord,
                        ref matchWildCards, ref matchSoundLike,
                        ref nmatchAllForms, ref forward,
                        ref wrap, ref format, ref replaceWithText,
                        ref replace, ref matchKashida,
                        ref matchDiactitics, ref matchAlefHamza,
                        ref matchControl);
        }

        private Word.Application CreateWordApplication()
        {
            Word.Application wordApp = new Word.Application();
            DateTime today = DateTime.Now;
            wordApp.Visible = false;
            return wordApp;
        }

        private Word.Document CreateWordDocument(Word.Application wordApp, object filename)
        {
            if (File.Exists((string)filename))
            {
                object readOnly = false;
                object inVisible = false;
                Word.Document aDoc = null;
                try
                {
                    aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                                            ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref missing,
                                            ref missing, ref missing, ref missing, ref missing);

                    aDoc.Activate();
                }
                catch (Exception) { }
                return aDoc;
            }
            else
            {
                return null;
            }
        }

        private void PrintDocument(Word.Application wordApp, Word.Document aDoc)
        {
            object copies = "1";
            object pages = "1";
            object range = Word.WdPrintOutRange.wdPrintCurrentPage;
            object items = Word.WdPrintOutItem.wdPrintDocumentContent;
            object pageType = Word.WdPrintOutPages.wdPrintAllPages;
            object oTrue = true;
            object oFalse = false;
            Word.Document document = aDoc;
            document.Activate();
            object nullobj = Missing.Value;
            int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref nullobj);
            wordApp.Visible = false;
            if (dialogResult == 1)
            {
                document.PrintOut(ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue, ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
            }
        }

        private void SaveAsFile(Word.Document aDoc, object saveAs)
        {
            aDoc.SaveAs2(ref saveAs, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }

        private void CloseDocument(Word.Application wordApp, Word.Document aDoc)
        {
            object saveChanged = false;
            aDoc.Close(ref saveChanged, ref missing, ref missing);
            wordApp.Quit();
        }
        public void InBaoCaoThamDinhTinDung(HoSo hs)
        {
            if (hs == null) return;
            Word.Application wordApp = CreateWordApplication();
            Word.Document aDoc = CreateWordDocument(wordApp, BAO_CAO_THAM_DINH_TIN_DUNG_TEMPLATE_PATH);
            if (aDoc == null)
            {
                MessageBox.Show("Không tìm thấy file báo cáo mẫu");
                return;
            }
            // find and replace
            this.FindAndReplace(wordApp, "<So>", hs.SoThamDinh.ToString());
            this.FindAndReplace(wordApp, "<TenKH>", hs.TenKH);
            this.FindAndReplace(wordApp, "<DiaChi>", hs.DiaChi);
            this.FindAndReplace(wordApp, "<TenNV>", hs.NhanVienCBTDTD.TenNV);
            this.FindAndReplace(wordApp, "<ChiNhanh>", hs.NhanVienCBTDTD.ChiNhanh);
            this.FindAndReplace(wordApp, "<NangLucPLDSHVDS>", hs.NangLucPLDSHVDS);
            this.FindAndReplace(wordApp, "<MucDichSDVV>", hs.MucDichSDVV);
            this.FindAndReplace(wordApp, "<KhaNangTC>", hs.KhaNangTC);
            this.FindAndReplace(wordApp, "<TinhKhaThiHQDA>", hs.TinhKhaThiHQDA);
            this.FindAndReplace(wordApp, "<TSDamBaoTV>", hs.TSDamBaoTV);
            this.FindAndReplace(wordApp, "<YKienCBTD>", hs.YKienCBTD);
            this.FindAndReplace(wordApp, "<YKienGiamDoc>", hs.YKienGiamDoc);
            DateTime ngay = hs.NgayThamDinh.Value;
            this.FindAndReplace(wordApp, "<Ngay>", ngay.Day.ToString());
            this.FindAndReplace(wordApp, "<Thang>", ngay.Month.ToString());
            this.FindAndReplace(wordApp, "<Nam>", ngay.Year.ToString());
            // print
            PrintDocument(wordApp, aDoc);
            // save
            // this.SaveAsFile(aDoc, @"D:\Work\Trang Project\Trang's Project\QuanLyChoVayCaNhan\bin\Debug\Templates\baocaothamdinh(" + hs.SoThamDinh+")");
            CloseDocument(wordApp, aDoc);
        }
        public void InBienBanDinhGiaTaiSan(TaiSan ts)
        {
            if (ts == null) return;
            Word.Application wordApp = CreateWordApplication();
            Word.Document aDoc = CreateWordDocument(wordApp, BIEN_BAN_DINH_GIA_TAI_SAN_TEMPLATE_PATH);
            if (aDoc == null)
            {
                MessageBox.Show("Không tìm thấy file biên bản mẫu");
                return;
            }
            // find and replace
            this.FindAndReplace(wordApp, "<So>", ts.SoTSTC.ToString());
            this.FindAndReplace(wordApp, "<TenKH>", ts.HoSo.TenKH);
            this.FindAndReplace(wordApp, "<SoCMND>", ts.HoSo.SoCMND);
            this.FindAndReplace(wordApp, "<NgayCap>", ts.HoSo.NgayCap);
            this.FindAndReplace(wordApp, "<NoiCap>", ts.HoSo.NoiCap);
            this.FindAndReplace(wordApp, "<DiaChi>", ts.HoSo.DiaChi);
            this.FindAndReplace(wordApp, "<ChiNhanh>", ts.NhanVienHTQHKH.ChiNhanh);
            this.FindAndReplace(wordApp, "<NganHang>", ts.NhanVienHTQHKH.NganHang);
            this.FindAndReplace(wordApp, "<TenNV>", ts.NhanVienHTQHKH.TenNV);
            this.FindAndReplace(wordApp, "<ChucVu>", ts.NhanVienHTQHKH.ChucVu);
            this.FindAndReplace(wordApp, "<MaTS>", ts.MaTS);
            this.FindAndReplace(wordApp, "<TenTS>", ts.TenTS);
            this.FindAndReplace(wordApp, "<GiaTri>", ts.GiaTri);
            DateTime ngay = ts.NgayDinhGia;
            this.FindAndReplace(wordApp, "<Ngay>", ngay.Day.ToString());
            this.FindAndReplace(wordApp, "<Thang>", ngay.Month.ToString());
            this.FindAndReplace(wordApp, "<Nam>", ngay.Year.ToString());
            PrintDocument(wordApp, aDoc);
            CloseDocument(wordApp, aDoc);
        }

        public void InGiayTiepNhanHoSo(TiepNhanHS tiepNhan)
        {
            Word.Application wordApp = CreateWordApplication();
            Word.Document aDoc = CreateWordDocument(wordApp, GIAY_TIEP_NHAN_HO_SO_TEMPLATE_PATH);
            if (aDoc == null)
            {
                MessageBox.Show("Không tìm thấy file báo cáo mẫu");
                return;
            }
            // find and replace
            this.FindAndReplace(wordApp, "<So>", tiepNhan.So.ToString());
            this.FindAndReplace(wordApp, "<TenKH>", tiepNhan.HoSo.TenKH);
            this.FindAndReplace(wordApp, "<SoCMND>", tiepNhan.HoSo.SoCMND);
            this.FindAndReplace(wordApp, "<NgayCap>", tiepNhan.HoSo.NgayCap);
            this.FindAndReplace(wordApp, "<NoiCap>", tiepNhan.HoSo.NoiCap);
            this.FindAndReplace(wordApp, "<DiaChi>", tiepNhan.HoSo.DiaChi);
            this.FindAndReplace(wordApp, "<DienThoai>", tiepNhan.HoSo.DienThoai);
            this.FindAndReplace(wordApp, "<ChiNhanh>", tiepNhan.HoSo.NhanVienQHKH.ChiNhanh);
            this.FindAndReplace(wordApp, "<NganHang>", tiepNhan.HoSo.NhanVienQHKH.NganHang);
            this.FindAndReplace(wordApp, "<TenNV>", tiepNhan.HoSo.NhanVienQHKH.TenNV);
            this.FindAndReplace(wordApp, "<ChucVu>", tiepNhan.HoSo.NhanVienQHKH.ChucVu);
            this.FindAndReplace(wordApp, "<CacTLTrongHS>", tiepNhan.CacTLTrongHS);
            this.FindAndReplace(wordApp, "<CacTKCanBS>", tiepNhan.CacTLCanBS);
            DateTime ngay = tiepNhan.NgayTiepNhan;
            this.FindAndReplace(wordApp, "<Ngay>", ngay.Day.ToString());
            this.FindAndReplace(wordApp, "<Thang>", ngay.Month.ToString());
            this.FindAndReplace(wordApp, "<Nam>", ngay.Year.ToString());
            PrintDocument(wordApp, aDoc);
            CloseDocument(wordApp, aDoc);
        } 
    }
}
